package com.ljfiction.book.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ljfiction.book.Baner.ShuqianBean;
import com.ljfiction.book.R;
import com.ljfiction.book.activitys.Reading2Activity;

import com.ljfiction.book.tools.ScreenUtils;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.SharedPreferencesUtil;

import com.yanzhenjie.recyclerview.swipe.SwipeItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Android on 2017/10/10.
 */

public class ShuQianBookdatas extends BaseFragment{
    @BindView(R.id.shiqianrecyclerview)
    SwipeMenuRecyclerView shiqianrecyclerview;
    Unbinder unbinder;

    private Context context;
    private String bookid;
    private List<ShuqianBean> datas = new ArrayList<>();
    private BaseQuickAdapter shuqianAdapter;


    public ShuQianBookdatas(){

    }
    public static ShuQianBookdatas newInstance(Context context, String bookid) {
        ShuQianBookdatas newFragment = new ShuQianBookdatas();
        Bundle bundle = new Bundle();
        bundle.putString("booid",bookid);
        newFragment.setArguments(bundle);
        return newFragment;

    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            this.context = getContext();
            this.bookid=args.getString("booid");
            datas = SettingManager.getInstance().getShuqian(bookid);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.shuqian, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        // 设置菜单创建器。
        shiqianrecyclerview.setSwipeMenuCreator(swipeMenuCreator);
        // 设置布局管理器
        shiqianrecyclerview.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        // 设置adapter
        shiqianrecyclerview.setAdapter(shuqianAdapter = new BaseQuickAdapter<ShuqianBean, BaseViewHolder>(R.layout.shuqianitem, datas) {
            @Override
            protected void convert(BaseViewHolder helper, ShuqianBean item) {
                AutoUtils.autoSize(helper.itemView);
                helper.setText(R.id.chaptertitleshuqian, item.getChapterTitle())
                        .setText(R.id.baifenbishuqian, item.getBaifenbi())
                        .setText(R.id.firstsetens, item.getFirstcontext())
                        .setText(R.id.shuqiantime, item.getShuqiantime());
            }
        });
// 设置菜单Item点击监听。
        shiqianrecyclerview.setSwipeMenuItemClickListener(new SwipeMenuItemClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge) {
                datas.remove(menuBridge.getAdapterPosition());
                shuqianAdapter.notifyItemRemoved(menuBridge.getPosition());
                shuqianAdapter.notifyDataSetChanged();
                shiqianrecyclerview.smoothCloseMenu();
                SettingManager.getInstance().setShuqian(bookid, datas);
            }
        });
        shiqianrecyclerview.setSwipeItemClickListener(new SwipeItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                SettingManager.getInstance().saveReadProgress(bookid,datas.get(position).getBaocunjilu()[0],datas.get(position).getBaocunjilu()[1],datas.get(position).getBaocunjilu()[2],datas.get(position).getBaocunjilu()[3],datas.get(position).getBaocunjilu()[4]);
                Intent intent=new Intent(context,Reading2Activity.class);
                intent.putExtra("bookId",bookid);
                startActivity(intent);
            }
        });
    }

 /***
     * 菜单创建器。在Item要创建菜单的时候调用。
     */
    private SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {

            /*SwipeMenuItem addItem = new SwipeMenuItem(context)
                    .setBackgroundDrawable(R.drawable.selector_green)// 点击的背景。
                    .setImage(R.mipmap.ic_action_add) // 图标。
                    .setWidth(size) // 宽度。
                    .setHeight(size); // 高度。
            swipeLeftMenu.addMenuItem(addItem); // 添加一个按钮到左侧菜单。*/

            SwipeMenuItem deleteItem = new SwipeMenuItem(context)
                    .setBackground(R.drawable.topbar)
                    .setText("删除") // 文字。
                    .setTextColor(Color.WHITE) // 文字颜色。
                    .setTextSize(16) // 文字大小。
                    .setWidth(ScreenUtils.getNum(context,60))
                    .setHeight(ScreenUtils.getNum(context,
                            50))
                    .setBackgroundColor(Color.parseColor("#f77583"));
            swipeRightMenu.addMenuItem(deleteItem);// 添加一个按钮到右侧侧菜单。


            // 上面的菜单哪边不要菜单就不要添加。
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void gengxin() {
        if (shuqianAdapter!=null){
            datas.clear();
            datas.addAll(SettingManager.getInstance().getShuqian(bookid));
            shuqianAdapter.notifyDataSetChanged();
        }

    }

}
